package config

import (
	"fmt"
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

type Driver struct {
	Root    string
	Pass    string
	Host    string
	Port    int
	DBName  string
	Timeout string
}

func init() {

	driver := Driver{
		Root:    "root",
		Pass:    "123456",
		Host:    "127.0.0.1",
		Port:    3306,
		DBName:  "clock",
		Timeout: "10s",
	}

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=%s",
		driver.Root,
		driver.Pass,
		driver.Host,
		driver.Port,
		driver.DBName,
		driver.Timeout)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		SkipDefaultTransaction: false,
	})

	if err != nil {
		log.Println("failed to connect database, err: ", err)
	}

	DB = db
	// 这里可以添加其他的数据库配置，比如连接池等
}
